package chuyentrung.racehorses;

import javax.microedition.khronos.opengles.GL10;

import chuyentrung.racehorses.World.WorldListener;
import chuyentrung.racehorses.framework.Game;
import chuyentrung.racehorses.framework.gl.Camera2D;
import chuyentrung.racehorses.framework.gl.FPSCounter;
import chuyentrung.racehorses.framework.gl.SpriteBatcher;
import chuyentrung.racehorses.framework.impl.GLScreen;
import chuyentrung.racehorses.framework.math.Rectangle;
import chuyentrung.racehorses.framework.math.Vector2;

public class PlayScreen extends GLScreen {
	private static enum StateGame {
		READY, RUNNING, PAUSED, FINISH;
	}

	private StateGame state;
	private Camera2D guiCam;
	private Vector2 touchPoint;
	private SpriteBatcher batcher;
	private World world;
	private WorldListener worldListener;
	private WorldRender render;
	private Rectangle soundBounds;
	private Rectangle resumeBounds;
	private Rectangle quitBounds;
	private Rectangle pauseBounds;
	private String timeString;
	private FPSCounter fpsCounter;

	public PlayScreen(Game game) {
		super(game);
		// TODO Auto-generated constructor stub

		state = StateGame.READY;
		guiCam = new Camera2D(glGraphics, GamePreferences.widthScreen,
				GamePreferences.heightScreen);
		touchPoint = new Vector2();
		batcher = new SpriteBatcher(glGraphics, 1000);
		worldListener = new WorldListener() {

			@Override
			public void hit() {
				// TODO Auto-generated method stub
				Assets.playSound(Assets.atkSnd);
			}

			@Override
			public void tossDice() {
				// TODO Auto-generated method stub
				Assets.playSound(Assets.tossDiceSnd);
			}

			@Override
			public void move() {
				// TODO Auto-generated method stub
				Assets.playSound(Assets.moveSnd);
			}

		};
		world = new World(worldListener);
		render = new WorldRender(glGraphics, batcher, world);
		quitBounds = new Rectangle(0, 0, 100, 100);
		resumeBounds = new Rectangle(0, 0, 100, 100);
		pauseBounds = new Rectangle(0, 0, 100, 100);
		soundBounds = new Rectangle(0, 0, 100, 100);
		timeString = "00 : 00";
		fpsCounter = new FPSCounter();

	}

	@Override
	public void update(float deltaTime) {
		// TODO Auto-generated method stub
		if (deltaTime > 0.1f)
			deltaTime = 0.1f;

		switch (state) {
		case READY:
			updateReady();
			break;

		case RUNNING:
			updateRunning(deltaTime);
			break;

		case PAUSED:
			updatePaused();
			break;

		case FINISH:
			updateFinish();
			break;
		}
	}

	private void updateReady() {

	}

	private void updateRunning(float deltaTime) {

	}

	private void updatePaused() {

	}

	private void updateFinish() {

	}

	@Override
	public void present(float deltaTime) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_TEXTURE_2D);
		
		render.render();
		guiCam.setViewportAndMatrices();
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		batcher.beginBatch(Assets.items);
		
		switch(state) {
		case READY :
			presentReady();
			break;
			
		case RUNNING :
			presentRunning();
			break;
			
		case PAUSED :
			presentPaused();
			break;
			
		case FINISH :
			presentFinish();
			break;
			
		}
		batcher.endBatch();
		gl.glDisable(GL10.GL_BLEND);
		fpsCounter.logFrame();		
	}
	
	
	public void presentReady() {
		batcher.drawSprite(0, 0, 200, 100, Assets.ready);
	}
	
	
	public void presentRunning() {
		
	}
	
	
	public void presentPaused() {
		
		
	}
	
	
	public void presentFinish() {
		
	}

	
	@Override
	public void pause() {
		// TODO Auto-generated method stub
		if (state == StateGame.RUNNING)
			state = StateGame.PAUSED;
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub		
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
